package service.Impl;

import dao.*;
import entity.*;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.repertory;
import service.MarketArauditorDao;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class MarketArauditorImpl implements MarketArauditorDao {
    @Autowired
    public MarketDao marketDao;//销售临时表dao层
    @Autowired
    public userDao userDao;//用户dao层
    @Autowired
    public ArauditorDao arauditorDao;//销售表dao层
    @Autowired
    public ArauditorDetailsDao arauditorDetailsDao;//销售详情表dao层
    @Autowired
    public RepertoryDao repertoryDao;//库存表dao层
    @Autowired
    public collectDao collectDao;//经济表dao层
    @Autowired
    public detailsDao detailsDao;//经济详情表dao层
    @Override
    public void success(String userName, String iphoneNum, int radio, double totals, int nums, double price, int zf, HttpServletRequest request) {
        //判断该用户是否在本店买过东西
        User user= userDao.flagUser(userName,iphoneNum);
        int uId;
        if(user!=null){//等于true的话取出用户编号
            uId=user.getUserId();
        }else{//等于false的话先添加该用户在取出编号
            userDao.addUser(userName,iphoneNum);
            User users=userDao.flagUser(userName,iphoneNum);
            uId=users.getUserId();
        }
        //取出正在登录的管理员作为经办人的编号
        int Aid=(int)request.getSession().getAttribute("Aid");
        //取出正在登录的管理员所有信息
        Administrator administrator=(Administrator) request.getSession().getAttribute("ad");
        //生成10位随机数
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("MMddHHmmss");
        String data=df.format(day);//生成订单号
        String dindan="XS"+data;//生成销售订单号
        //往销售审实体类中添加数据
        Arauditor arauditor =new Arauditor();
        arauditor.setSeat(dindan);
        arauditor.setAid(Aid);
        arauditor.setBmId(zf);
        arauditor.setPresetPrice(totals);
        arauditor.setPrice(price);
        arauditor.setUserId(uId);
        arauditor.setSums(nums);
        arauditor.setStateid(radio);
        if(radio==1){//代表已审核可以添加审核人
           arauditor.setaRAuditorId(Aid);
           arauditor.setVerifier(administrator.getAname());
        }
        //往销售审核表中添加数据
        arauditorDao.addArauditor(arauditor);
        //取出用户购买的东西
        List<Market> list=marketDao.showMarkets();
        //往销售详情表中添加数据
        //循环读取用户购买的东西
        ArauditorDetails ad=null;//创建一个空的销售详情实体类
        repertory repertory=null;//创建一个空的库存实体类pojo包下的
        for (entity.Market market:list){
            ad=new ArauditorDetails();//循环给销售详情实体类存放数据
            ad.setAdNum(market.getMarketNum());
            ad.setAdTotal(market.getTotalPrice());
            ad.setSeat(dindan);
            ad.setRepertoryId(market.getRepertory().getRepertoryId());
            //循环读取销售临时表中的根据销售详情实体类往销售详情表中添加数据
            arauditorDetailsDao.addArauditorDetails(ad);
            //循环往库存实体类里面添加信息
            repertory=new repertory();
            repertory.setRepertoryId(market.getRepertory().getRepertoryId());
            repertory.setNums(market.getMarketNum());
            repertory.setTotal(market.getTotalPrice());
            //根据库存实体类pojo包下的更改库存数量和库存预计总价钱
            repertoryDao.uprepertoryNum(repertory);
            //判断是否审核未审核不能往经济表中添加数据
            if(radio==1){//代表已审核
                //根据用户姓名和用户电话判断经济表中是否有该用户购买的记录
                Collect collect= collectDao.flagCollect(userName,iphoneNum);
                if(collect!=null){
                    //collect不等于null代表有值根据该实体类里面的编号修改我方应收金额
                    collectDao.upCollectMyprice(collect.getCollectid(),price);
                }else{//collect等于null代表没值进行往经济表中添加数据
                    //创建一个经济表实体类往里面录入值
                    Collect collect1=new Collect();
                    collect1.setBtype(userName);
                    collect1.setFlatType("客户");
                    collect1.setHeprice(0.00);
                    collect1.setMyprice(price);
                    collect1.setIphoneNum(iphoneNum);
                    //根据经济表实体类往经济表中添加数据
                    collectDao.addCollect(collect1);
                }
                //往经济详情实体类中录入数据
                Details details=new Details();
                details.setAid(Aid);
                details.setAmount(totals);
                details.setBtype(userName);
                details.setPaid(price);
                details.setIphoneNum(iphoneNum);
                details.setSynopsis("商品销售付款");
                details.setSeat(dindan);
                detailsDao.addDetails(details);//根据经济详情实体类往经济详情表中添加数据
                //删除销售临时表中的数据
                marketDao.delmarkets();
                System.out.println("删除成功");
            }
        }
    }

    @Override
    public void shenhe(String seat, HttpServletRequest request) {
        //取出正在登录的管理员所有信息
        Administrator administrator=(Administrator) request.getSession().getAttribute("ad");
        arauditorDao.upStateSeat(seat,administrator.getAname());//根据单号进行修改
        Arauditor arauditor=arauditorDao.showSeatArauditor(seat);//根据单号查询该信息
        //创建一个经济表实体类往里面录入值
        Collect collect1=new Collect();
        //根据单号查询的信息往该实体类里面进行录入值
        collect1.setBtype(arauditor.getUser().getUserName());
        collect1.setFlatType("客户");
        collect1.setHeprice(0.00);
        collect1.setMyprice(arauditor.getPrice());
        collect1.setIphoneNum(arauditor.getUser().getIphoneNum());
        //根据经济表实体类往经济表中添加数据
        collectDao.addCollect(collect1);
        //往经济详情实体类中录入数据
        Details details=new Details();
        int Aid=(int)request.getSession().getAttribute("Aid");
        details.setAid(Aid);
        details.setAmount(arauditor.getPresetPrice());
        details.setBtype(arauditor.getUser().getUserName());
        details.setPaid(arauditor.getPrice());
        details.setIphoneNum(arauditor.getUser().getIphoneNum());
        details.setSynopsis("商品销售付款");
        details.setSeat(seat);
        detailsDao.addDetails(details);//根据经济详情实体类往经济详情表中添加数据
        System.out.println("审核成功");
    }
}
